﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace OALPTool
{
    public partial class DataGridViewHeadDemo : Form
    {

        #region 自定义变量

        /// <summary>
        /// DataGridViewHead数据源 
        /// </summary>
        private DataView dv = new DataView();
        
        #endregion

        public DataGridViewHeadDemo()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public int InitForm() 
        {
            this.InitHeader();
            this.InitTable();
            this.SetDgvStyle();
            return 0;
        }



        #region 数据源初始化

        /// <summary>
        /// 初始化表头
        /// </summary>
        /// <returns></returns>
        public int InitHeader()
        {
            //表头级数
            this.dgvHead.ColumnDeep = 3;

            TreeView tv = new TreeView();

            tv.Nodes.Add("1", "银行名称");

            tv.Nodes.Add("2", "只开不贴");
            tv.Nodes["2"].Nodes.Add("21", "商票");
            tv.Nodes["2"].Nodes["21"].Nodes.Add("211", "笔数");
            tv.Nodes["2"].Nodes["21"].Nodes.Add("212", "票面金额");
            tv.Nodes["2"].Nodes["21"].Nodes.Add("213", "手续费");

            tv.Nodes["2"].Nodes.Add("22", "银票");
            tv.Nodes["2"].Nodes["22"].Nodes.Add("221", "笔数");
            tv.Nodes["2"].Nodes["22"].Nodes.Add("222", "票面金额");

            tv.Nodes.Add("3", "即开即贴");
            tv.Nodes["3"].Nodes.Add("31", "商票");
            tv.Nodes["3"].Nodes["31"].Nodes.Add("311", "笔数");
            tv.Nodes["3"].Nodes["31"].Nodes.Add("312", "票据金额");

            TreeView[] tvs = new TreeView[1];
            tvs[0] = tv;

            this.dgvHead.ColumnTreeView = tvs;

            return 0;
        }


        /// <summary>
        /// 初始化DataGridView数据源
        /// </summary>
        public void InitTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("xm1", typeof(String));
            dt.Columns.Add("xm2", typeof(Double));
            dt.Columns.Add("xm3", typeof(Double));
            dt.Columns.Add("xm4", typeof(Double));
            dt.Columns.Add("xm5", typeof(Double));
            dt.Columns.Add("xm6", typeof(Double));
            dt.Columns.Add("xm7", typeof(Double));
            dt.Columns.Add("xm8", typeof(Double));

            DataRow dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["xm1"] = "中国银行";
            dr["xm2"] = "1";
            dr["xm3"] = "2300";
            dr["xm4"] = "100";
            dr["xm5"] = "4";
            dr["xm6"] = "32122";
            dr["xm7"] = "1";
            dr["xm8"] = "1000";
            dt.Rows.Add(dr);

            dt.AcceptChanges();

            this.dv = dt.DefaultView;

        }

        /// <summary>
        /// 设置DataGridView样式
        /// </summary>
        private void SetDgvStyle()
        {
            DataGridViewFunc.setDgvStyle(this.getColList(), ref this.dgvHead, this.dv);

            this.dgvHead.AutoGenerateColumns = false;
            this.dgvHead.AllowUserToAddRows = false;
            this.dgvHead.AllowUserToDeleteRows = false;

            this.dgvHead.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //不可排序
            foreach (DataGridViewColumn dgCol in this.dgvHead.Columns)
            {
                dgCol.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        /// <summary>
        /// 获取DataGridView样式列表
        /// </summary>
        /// <returns>返回样式列表</returns>
        private ArrayList getColList()
        {

            ArrayList colList = new ArrayList();
            ///   0:字段中文名；
            ///   1:字段英文名；
            ///   2:值类型【0-TextBox(默认),1-CheckBox,2-ComboBox,3-DateTimePicker,6-TextBox(自动换行),7-超链接】；
            ///   3:显示宽度；
            ///   4:显示格式化【"c"表示数字格式化；"d"表示时间格式化】；
            ///   5:对齐方式；
            ///   6:是否只读【true为只读；false为可修改】；
            ///   7:数据为空时显示；
            ///   8:是否存在转义；
            ///   9:显示顺序号；
            ///   10:颜色字段名【暂时未用】；
            ///   11:颜色字段意义列表【暂时未用】；
            ///   12:格式化字段；
            ///   13:暂时没用；
            ///   14:如果值类型为2，那么该值为Combol项集合对应数据集【每条数据对应一个object集合，object[0]为Display值，object[1]为Value值】            
            
            colList.Add(new object[] { "银行名称", "xm1", "0", 180, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 1, "", "", "", "", "" });
            colList.Add(new object[] { "笔数", "xm2", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 2, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "票面金额", "xm3", "0", 80, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 3, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "手续费", "xm4", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, false, "", null, 4, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "笔数", "xm5", "0", 80, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 5, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "票面金额", "xm6", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 6, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "笔数", "xm7", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 7, "", "", "#,##0.00", "", "" });
            colList.Add(new object[] { "票面金额", "xm8", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 8, "", "", "#,##0.00", "", "" });

            return colList;
        }

        /// <summary>
        /// 导出事件
        /// </summary>
        private void tbbExcel_Click(object sender, EventArgs e)
        {
            //构建报表表头
            ArrayList list = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("BegRow", typeof(int));
            dt.Columns.Add("EndRow", typeof(int));
            dt.Columns.Add("BegCol", typeof(int));
            dt.Columns.Add("EndCol", typeof(int));
            dt.Columns.Add("Text", typeof(string));

            list = new ArrayList();
            ArrayList list11 = new ArrayList();

            DataRow dr11 = dt.NewRow();
            dr11["BegRow"] = 1;
            dr11["EndRow"] = 3;
            dr11["BegCol"] = 1;
            dr11["EndCol"] = 1;
            dr11["Text"] = "银行名称";
            list11.Add(dr11);

            DataRow dr12 = dt.NewRow();
            dr12["BegRow"] = 1;
            dr12["EndRow"] = 1;
            dr12["BegCol"] = 2;
            dr12["EndCol"] = 6;
            dr12["Text"] = "只开不贴";
            list11.Add(dr12);

            DataRow dr13 = dt.NewRow();
            dr13["BegRow"] = 1;
            dr13["EndRow"] = 1;
            dr13["BegCol"] = 7;
            dr13["EndCol"] = 8;
            dr13["Text"] = "即开即贴";
            list11.Add(dr13);

            list.Add(list11);

            ArrayList list12 = new ArrayList();

            DataRow dr21 = dt.NewRow();
            dr21["BegRow"] = 2;
            dr21["EndRow"] = 2;
            dr21["BegCol"] = 2;
            dr21["EndCol"] = 4;
            dr21["Text"] = "商票";
            list12.Add(dr21);

            DataRow dr22 = dt.NewRow();
            dr22["BegRow"] = 2;
            dr22["EndRow"] = 2;
            dr22["BegCol"] = 5;
            dr22["EndCol"] = 6;
            dr22["Text"] = "银票";
            list12.Add(dr22);

            DataRow dr23 = dt.NewRow();
            dr23["BegRow"] = 2;
            dr23["EndRow"] = 2;
            dr23["BegCol"] = 7;
            dr23["EndCol"] = 8;
            dr23["Text"] = "商票";
            list12.Add(dr23);

            list.Add(list12);

            ArrayList list13 = new ArrayList();
            DataRow dr31 = dt.NewRow();
            dr31["BegRow"] = 3;
            dr31["EndRow"] = 3;
            dr31["BegCol"] = 2;
            dr31["EndCol"] = 2;
            dr31["Text"] = "笔数";
            list13.Add(dr31);

            DataRow dr32 = dt.NewRow();
            dr32["BegRow"] = 3;
            dr32["EndRow"] = 3;
            dr32["BegCol"] = 3;
            dr32["EndCol"] = 3;
            dr32["Text"] = "票面金额";
            list13.Add(dr32);

            DataRow dr33 = dt.NewRow();
            dr33["BegRow"] = 3;
            dr33["EndRow"] = 3;
            dr33["BegCol"] = 4;
            dr33["EndCol"] = 4;
            dr33["Text"] = "手续费";
            list13.Add(dr33);

            DataRow dr35 = dt.NewRow();
            dr35["BegRow"] = 3;
            dr35["EndRow"] = 3;
            dr35["BegCol"] = 5;
            dr35["EndCol"] = 5;
            dr35["Text"] = "笔数";
            list13.Add(dr35);

            DataRow dr36 = dt.NewRow();
            dr36["BegRow"] = 3;
            dr36["EndRow"] = 3;
            dr36["BegCol"] = 6;
            dr36["EndCol"] = 6;
            dr36["Text"] = "票面金额";
            list13.Add(dr36);

            DataRow dr39 = dt.NewRow();
            dr39["BegRow"] = 3;
            dr39["EndRow"] = 3;
            dr39["BegCol"] = 7;
            dr39["EndCol"] = 7;
            dr39["Text"] = "笔数";
            list13.Add(dr39);

            DataRow dr310 = dt.NewRow();
            dr310["BegRow"] = 3;
            dr310["EndRow"] = 3;
            dr310["BegCol"] = 8;
            dr310["EndCol"] = 8;
            dr310["Text"] = "票面金额";
            list13.Add(dr310);

            list.Add(list13);



            //Excel标题
            DataTable dtTitle = new DataTable();
            dtTitle.Columns.Add("BegRow", typeof(int));
            dtTitle.Columns.Add("EndRow", typeof(int));
            dtTitle.Columns.Add("BegCol", typeof(int));
            dtTitle.Columns.Add("EndCol", typeof(int));
            dtTitle.Columns.Add("Text", typeof(string));
            dtTitle.Columns.Add("Align", typeof(string));

            ArrayList titleList = new ArrayList();
            //第一行标题
            ArrayList titlelt1 = new ArrayList();

            DataRow drT1 = dtTitle.NewRow();
            drT1["BegRow"] = 1;
            drT1["EndRow"] = 1;
            drT1["BegCol"] = 1;
            drT1["EndCol"] = 8;
            drT1["Text"] = "票据信息";
            drT1["Align"] = "0";

            titlelt1.Add(drT1);

            titleList.Add(titlelt1);

            //第二行标题
            ArrayList titlelt2 = new ArrayList();

            DataRow drT2 = dtTitle.NewRow();
            drT2["BegRow"] = 2;
            drT2["EndRow"] = 2;
            drT2["BegCol"] = 1;
            drT2["EndCol"] = 8;
            drT2["Text"] = System.DateTime.Now.ToShortDateString();
            drT2["Align"] = "2";

            titlelt2.Add(drT2);

            titleList.Add(titlelt2);

            ArrayList Args = new ArrayList();
            object objs = null;
            int ret = 0;
            Args.Add(this.dgvHead);
            Args.Add(list);
            Args.Add(titleList);
            ret = TSysPublic.ExecFunction("SAL_DataGridViewHeadToExcel", ref Args, out objs);
            if (ret < 0)
            {
                TSysPublic.ShowMsg("导出到Excel失败!");
            }

        }


        #endregion

    }
}
